﻿using System.Collections.Generic;
using UnityEngine;

namespace UnityView
{
    /// <summary>
    /// View 管理器，存放管理视图的基本信息
    /// </summary>
    public class UIViewManager : MonoBehaviour
    {
        public static UIViewManager Instance = null;

        // 动画管理
        protected Dictionary<int, AnimationGroup> AliveAnimation = new Dictionary<int, AnimationGroup>();
        protected List<AnimationGroup> ObsoleteAnimation = new List<AnimationGroup>();

        public void AppendAnimation(AnimationGroup group)
        {
            AliveAnimation.Add(group.GroupID, group);
        }

        protected void Awake()
        {
            Instance = this;
        }

        public void Update()
        {
            if (Toasting)
            {
                UpdateToast();
            }

            // foreach (var group in AliveAnimation.Values)
            // {
            //     group.Update();
            //     if (group.IsFinish()) ObsoleteAnimation.Add(group);
            // }
            // if (ObsoleteAnimation.Count > 0)
            // {
            //     foreach (var group in ObsoleteAnimation)
            //     {
            //         AliveAnimation.Remove(group.GroupID);
            //         group.Finish();
            //     }
            //     ObsoleteAnimation.Clear();
            // }

            if (UIAnimation.Animating)
            {
                UIAnimation.Update();
            }
        }

        #region Toast

        protected static bool Toasting = false;
        protected readonly Queue<UIToastInfo> ToastQueue = new Queue<UIToastInfo>();
        protected UIToastInfo CurrentToast = null;
        protected UIToast Toast = null;

        public void ShowToast(UIToastInfo toastInfo)
        {
            if (Toast == null) Toast = new UIToast();

            ToastQueue.Enqueue(toastInfo);

            if (toastInfo.ShowImmediately)
            {
                ToastTimer = 0;
                CurrentToast = null;
            }

            Toasting = true;
        }

        public void DiscardToast()
        {
            ToastQueue.Clear();
            NextToast();
        }

        protected float ToastTimer = 0;
        protected void UpdateToast()
        {
            if (CurrentToast == null)
            {
                if (!NextToast()) return;
            }
            ToastTimer += Time.deltaTime;

            if (ToastTimer < CurrentToast.ShowTime)
            {
                Toast.Visible = true;
                Toast.BackgroundColor = CurrentToast.BackgroundColor;
                Toast.TitleColor = CurrentToast.TextColor;
                return;
            }
            if (CurrentToast.Fade && (ToastTimer < CurrentToast.LifeTime))
            {
                float fadeTime = ToastTimer - CurrentToast.ShowTime;
                float d = fadeTime / CurrentToast.FadeTime;
                var cb = CurrentToast.BackgroundColor;
                var ct = CurrentToast.TextColor;
                Toast.BackgroundColor = new Color(cb.r, cb.g, cb.b, 1f - d);
                Toast.TitleColor = new Color(ct.r, ct.g, ct.b, 1f - d);
                return;
            }

            ToastTimer = 0;
            NextToast();
        }

        protected bool NextToast()
        {
            if (ToastQueue.Count == 0)
            {
                Toasting = false;
                Toast.Visible = false;
                return false;
            }
            Toasting = true;
            CurrentToast = ToastQueue.Dequeue();
            Toast.Title = CurrentToast.Text;
            return true;
        }

        #endregion
    }
}
